package com.accountant.travelaccountant;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.widget.EditText;
import android.widget.Toast;

public class Utils_NotActivity extends Activity {

	public static String string;
	public static SimpleDateFormat dateFormat;
	public static Date todays_date_or_time;
	public static boolean buttonPressed = true;

	public static String[] amountTableColumns() {
		String[] columns = {
				DatabaseOperations_NonActivity.getAmounttabletitle(),
				DatabaseOperations_NonActivity.getAmounttableamount(),
				DatabaseOperations_NonActivity.getAmounttabletotalmembers() };
		return columns;
	}

	public static String[] memberTableColumns() {
		String[] columns = {
				DatabaseOperations_NonActivity.getMembertabletitle(),
				DatabaseOperations_NonActivity.getMembertablemember(),
				DatabaseOperations_NonActivity.getMembertableamount(),
				DatabaseOperations_NonActivity.getMembertabletoget(),
				DatabaseOperations_NonActivity.getMembertabletogive() };
		return columns;
	}

	public static String[] purposeTableColumns() {
		String[] columns = {
				DatabaseOperations_NonActivity.getPurposetabletitle(),
				DatabaseOperations_NonActivity.getPurposetablepayingmember(),
				DatabaseOperations_NonActivity.getPurposetablepurpose(),
				DatabaseOperations_NonActivity.getPurposetableamount() };
		return columns;
	}

	public static String[] uniqueNamesTableColumns() {
		String[] columns = { DatabaseOperations_NonActivity
				.getUniquenamestablenames() };
		return columns;
	}

	/*
	 * Check if the database exists so that if database is deleted in between
	 * then it is detected
	 */
	public static boolean databaseDoesNotExist(Context context) {
		/* Getting the database name */
		String DatabaseName = context.getFilesDir().getParent() + "/databases/"
				+ DatabaseOperations_NonActivity.getDatabaseName();
		/* False means database does not exist is false i.e database does exist */
		File checkDB = new File(DatabaseName);
		return !checkDB.exists();
		/* return false */
	}

	public static void validateTextLength(EditText editText) {
		if (editText.getText().length() > getTextMaxLength(editText
				.getContext())) {
			string = editText.getText().toString()
					.substring(0, getTextMaxLength(editText.getContext()));
			editText.setText(string);
			Toast.makeText(
					editText.getContext(),
					"Maximum " + getTextMaxLength(editText.getContext())
							+ " characters are allowed", Toast.LENGTH_SHORT)
					.show();
			/* Setting the cursor right at the end */
			editText.setSelection(getTextMaxLength(editText.getContext()));
		}
	}

	public static void validateNumberTextLength(EditText editText) {
		if (editText.getText().length() > getNumberMaxLength(editText
				.getContext())) {
			final String str = editText.getText().toString();
			editText.setText(str.substring(0,
					getNumberMaxLength(editText.getContext())));
			Toast.makeText(
					editText.getContext(),
					"Maximum " + getNumberMaxLength(editText.getContext())
							+ " digits are allowed", Toast.LENGTH_SHORT).show();
			editText.setSelection(getNumberMaxLength(editText.getContext()));
		}
	}

	public static int getNumberMaxLength(Context context) {
		return Integer
				.parseInt(context.getString(R.string.maxNumberTextLength));
	}

	public static int getTextMaxLength(Context context) {
		return Integer.parseInt(context.getString(R.string.maxTextLength));
	}

	public static void setCursorPosition(EditText editText) {
		/* Setting the cursor position */
		editText.setSelection(editText.getText().length());
	}

	public static String getCurrentDate() {
		todays_date_or_time = new Date();
		dateFormat = new SimpleDateFormat("EEE dd/MM/yyyy");
		return dateFormat.format(todays_date_or_time);
	}

	public static String getCurrentTime() {
		todays_date_or_time = new Date();
		dateFormat = new SimpleDateFormat("hh:mm");
		return dateFormat.format(todays_date_or_time);
	}

	/*
	 * A function to prevent a button from being presed twice in case the button
	 * functions slowly
	 */
	/*
	 * How to use it: Just initialise the Utils.buttonPressed=true; first
	 * followed by calling this function; its just like test and set
	 */
	public static boolean saveButtonFromBeingPressedTwice() {
		if (buttonPressed) {
			buttonPressed = false;
			return true;
		} else {
			return false;
		}
	}

}
